﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using DTO;
namespace DAO
{
    public class ChiNhanhDAO : DataProvider
    {
        protected override object GetDataFromDataRow(DataTable dt, int i)
        {


            ChiNhanhDTO objectDTO = new ChiNhanhDTO();
            objectDTO.MaChiNhanh = int.Parse(dt.Rows[i]["MaChiNhanh"].ToString());
            objectDTO.TenChiNhanh = dt.Rows[i]["TenChiNhanh"].ToString();
            objectDTO.DiaChi = dt.Rows[i]["DiaChi"].ToString();
            objectDTO.GhiChu = dt.Rows[i]["GhiChu"].ToString();
            return (object)objectDTO;
        }

        public ChiNhanhDTO[] GetAll()
        {
            String strSQL = "Select * from ChiNhanh ";
            DataTable dt = new DataProvider().ExecuteQuery(strSQL);

           ChiNhanhDTO[] ChiNhanhDTOs = new ChiNhanhDTO[dt.Rows.Count];
            for (int i = 0; i < dt.Rows.Count; i++)
                ChiNhanhDTOs[i] = (ChiNhanhDTO)GetDataFromDataRow(dt, i);

            return ChiNhanhDTOs;
        }
        public DataTable GetAllDataTable()
        {
            String strSQL = "Select * from ChiNhanh ";
            DataTable dt = new DataProvider().ExecuteQuery(strSQL);
            int d = dt.Rows.Count; 
            return dt;
        }
       

        public bool insert(ChiNhanhDTO info)
        {
            string insertCommand = "INSERT CHINHANH VALUES(" +
                info.MaChiNhanh + ", '" +
                info.TenChiNhanh + ", '" +
                info.DiaChi + "', '" +
                info.GhiChu + "')";

           return ExecuteNonQuery(insertCommand);
        }

        public bool update(ChiNhanhDTO info)
        {
            string updateCommand = "UPDATE CHINHANH " +
                                    "SET TenChiNhanh = '" + info.TenChiNhanh+ "', " +
                                    " DiaChi = '" + info.DiaChi + "', " +
                                    " GhiChu = '" + info.GhiChu + "'" +
                                    " WHERE MaKhachHang = " + info.MaChiNhanh;


            return ExecuteNonQuery(updateCommand);
        }

        public bool delete(int maChiNhanh)
        {
            string deleteCommand = "DELETE FROM CHINHANH WHERE MaChiNhanh = " + maChiNhanh;
            return ExecuteNonQuery(deleteCommand);
        }
    }
}
